<template>
  <div class="app-container">
    <div style="margin-bottom: 10px">
      <span>荣耀解析管理</span>
    </div>
    <el-form
      ref="formData"
      size="small"
      :model="formData"
      :rules="rules"
      :inline="true"
      class="demo-form-inline"
      style="margin-left: 20px;"
    >
      <el-row>
        <el-col :span="4">
          <el-form-item label="标题:" prop="title">
            <el-input
              v-model="formData.title"
              placeholder="请输入标题"
              clearable
              @change="fetchData"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="来源:" prop="author">
            <el-select
              v-if="formData.status === 0"
              v-model="formData.author"
              clearable
              placeholder="请选择来源"
              @change="fetchData"
            >
              <el-option
                v-for="item in authorList"
                :key="item.author"
                :label="item.author"
                :value="item.author"
              />
            </el-select>
            <el-input
              v-else
              v-model="formData.author"
              placeholder="请输入来源"
              clearable
              @change="fetchData"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="奖项:" prop="prize">
            <el-select
              v-if="formData.status === 0"
              v-model="formData.prize"
              clearable
              placeholder="请选择奖项"
              @change="fetchData"
            >
              <el-option
                v-for="item in prizeList"
                :key="item.prize"
                :label="item.prize"
                :value="item.prize"
              />
            </el-select>
            <el-input
              v-else
              v-model="formData.prize"
              placeholder="请输入奖项"
              clearable
              @change="fetchData"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="formData.status"
              filterable
              placeholder="请选择状态"
              style="width: 200px"
              @change="fetchData"
            >
              <el-option label="未处理" :value="0" />
              <el-option label="已处理" :value="1" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" style=" margin-bottom: 8px;">
        <el-col :span="1.5">
          <el-button ref="fetchData" icon="el-icon-search" type="primary" size="mini" @click="fetchData">查询</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button ref="resetData" icon="el-icon-refresh" type="primary" size="mini" @click="resetData">重置</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-if="formData.status === 0"
            ref="ignore"
            size="mini"
            type="warning"
            icon
            @click="multipleHandle(1)"
          >
            <svg-icon icon-class="hide" style="margin-right: 3px; width: 13px; height: 13px" />
            批量处理</el-button>
          <el-button
            v-else
            ref="unIgnore"
            size="mini"
            type="danger"
            icon
            @click="multipleHandle(0)"
          >
            <svg-icon icon-class="return" style="margin-right: 3px; width: 13px; height: 13px" />
            批量撤销</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table
      ref="tableList"
      v-loading="loading"
      :data="list"
      :header-cell-style="headStyle"
      :cell-style="cellStyle"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="70" align="center" />
      <el-table-column prop="title" label="标题" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_blank" style="color: #328df3">{{ scope.row.title }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="author" label="来源网站" align="center" />
      <el-table-column prop="keyword" label="关键词" align="center" width="150" />
      <el-table-column prop="intime" label="入库时间" align="center" width="120" />
      <el-table-column prop="prize" label="奖项名称" align="center" />
      <el-table-column prop="person" label="负责人" align="center" width="100" />
      <el-table-column prop="status" label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.status"
            filterable
            placeholder="请选择状态"
            @change="updateData(scope.row.id, scope.row.status)"
          >
            <el-option label="未处理" :value="0" />
            <el-option label="已处理" :value="1" />
          </el-select>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      background
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[10, 20, 50, 100]"
      prev-text="上一页"
      next-text="下一页"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>

<script>
import variables from '@/styles/variables.scss'
import crawler from '@/api/crawler/crawler'

export default {
  name: 'Honor',
  data() {
    return {
      total: 0, // 数据库总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      formData: { // 表单数据
        title: '',
        author: '',
        status: 0,
        prize: ''
      },
      list: [], // 列表数据
      authorList: [], // 来源列表
      prizeList: [], // 奖项列表
      loading: false,
      rules: {

      }
    }
  },

  created() {
    this.fetchData()
    this.fetchList()
  },

  methods: {
    fetchData() {
      this.loading = true
      crawler.honorListPage(this.page, this.limit, this.formData).then((response) => {
        this.list = response.data.result.list
        this.total = response.data.result.total
        this.loading = false
      })
      this.fetchList()
      this.$nextTick(() => {
        this.$refs.fetchData.$el.blur()
      })
    },

    fetchList() {
      crawler.honorAuthorList().then((response) => {
        this.authorList = response.data.result
      })
      crawler.honorPrizeList().then((response) => {
        this.prizeList = response.data.result
      })
    },

    resetData() {
      this.formData = {
        title: '',
        author: '',
        status: 0,
        prize: ''
      }
      this.page = 1
      this.limit = 10
      this.fetchData()
    },

    updateData(id, status) {
      crawler.honorUpdateData(id, status).then((response) => {
        if (response.code === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.fetchData()
        }
      })
    },

    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 批量处理
    multipleHandle(status) {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请选择数据',
          type: 'warning'
        })
      } else {
        // 获取did集合
        const arr = []
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })

        // 创建表单
        const formData = new FormData()
        formData.append('status', status)
        formData.append('arr', arr)

        crawler.honorMultipleHandle(formData).then((response) => {
          if (response.code === 200) {
            this.$message({
              message: '处理成功',
              type: 'success'
            })
            this.fetchData()
          } else {
            this.$message({
              message: response.message,
              type: 'error'
            })
          }
        })
      }
      if (status === 0) {
        this.$nextTick(() => {
          this.$refs.ignore.$el.blur()
        })
      } else {
        this.$nextTick(() => {
          this.$refs.unIgnore.$el.blur()
        })
      }
    },

    // 每页展示条数的改变，size：回调函数, 表示当前选中的展示条数
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },

    // 翻页，page：回调函数, 表示当前选中的页码
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },

    // 表头style
    headStyle() {
      return 'color: black; backgroundColor: ' + variables.globalStyle
    },

    // 列style
    cellStyle({ row, column, rowIndex, columnIndex }) {
      return 'height: 50px'
    }
  }
}
</script>

<style scoped>

</style>
